+<?php
header("Content-type:text/html;charset=utf-8");

/*
*打印数据
*/
function p($data = [])
{
    echo '<pre>';
    print_r($data);
    echo '</pre>';
}

/*
*打印数据并退出
*/
function pp($data = [])
{
    p($data);
    exit;
}


class SQLiteDB
{
    public $db;

    /* 数据初始化 */
    public function __construct()
    {
        $file = dirname(__FILE__) . '\aaaSnsMicroMsg.db';
        try {
            $this->db = new PDO('sqlite:' . $file); // success
            // $a = $this->db->query("SET NAMES  utf8");
            // var_dump($a);
            // exit;
        } catch (Exception $e) {
            echo $e;
            exit;
        }
    }

    /*
    * 列出所有的表
    */
    public function allTable()
    {
        $all_table = $this->db->query("select name from sqlite_master where type='table' order by name");
        $all_table = $all_table->fetchAll(PDO::FETCH_ASSOC);
        $name = [];
        foreach ($all_table as $key => $value) {
            $name[] = $value['name'];
        }

        return $name;
    }

    /*
    * table 表结构
    */
    public function structureTable($table = [])
    {
        $temp = [];
        foreach ($table as $key => $value) {
            $sql = "select * from sqlite_master where type='table' and name=?";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(1, $value);
            $stmt->execute();
            $data = $stmt->fetch(PDO::FETCH_ASSOC);
            $temp[$value] = $data;
        }

        return $temp;
    }

    /*
    * table 表数据
    */
    public function dataTable($table = [])
    {
        $temp = [];
        foreach ($table as $key => $value) {
            $sql = "select * from " . $value;
            $execute = $this->db->query($sql);
            $data = $execute->fetchAll(PDO::FETCH_ASSOC);
            $temp[$value] = $data;
        }

        return $temp;
    }
}


$sqldb = new SQLiteDB;
$allTable = $sqldb->allTable();


$structureTable = $sqldb->structureTable($allTable);
// p($structureTable);


$dataTable = $sqldb->dataTable($allTable);
// p($dataTable);


?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>sqlite结构</title>

    <!-- Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>


<div class="container-fluid">
    <div class="row">
        <div class="col-md-2">
            <div class="list-group" style="position: fixed;top: 0px;z-index: 999;width: inherit;padding-right:10px;">
                <a href="#" class="list-group-item active">
                    数据表列表
                </a>
                <?php
                foreach ($allTable as $key => $value):
                    echo '<a href="#' . $value . '" class="list-group-item">' . $value . '</a>';
                endforeach;
                ?>
            </div>
        </div>
        <div class="col-md-10">
            <?php foreach ($structureTable as $key => $value): ?>
                <h3 id="<?php echo $key; ?>"><?php echo $key; ?></h3>
                <table class="table table-bordered  table-hover table-condensed">
                    <tr>
                        <?php
                        foreach ($value as $k => $v) {
                            if ($k == 'sql') {
                                continue;
                            }
                            echo '<th>';
                            echo $v;
                            echo '</th>';
                        }
                        ?>
                    </tr>
                    <?php foreach ($dataTable[$key] as $k => $v): ?>
                        <?php
                        echo "<tr>";
                        foreach ($v as $kk => $vv) {
                            echo "<td style='max-width: 300px;word-break:break-all;'>";
                            echo $vv;
                            echo "</td>";
                        }
                        echo "</tr>";
                        ?>
                    <?php endforeach ?>
                </table>
            <?php endforeach ?>

        </div>
    </div>
</div>


<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>


